using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Entity;
using APP.QUANLY.Entity.ConditionForm;

namespace APP.QUANLY.DAL
{
    /// <summary>
    /// Summary description for NHACUNGCAP.
     /// </summary>
	public class NHACUNGCAPDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public NHACUNGCAPDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public NHACUNGCAPDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT MANHACUNGCAP, TENNHACUNGCAP, DIACHI, DIENTHOAI, DIDONG, NGUOIDAIDIEN, EMAIL, THEODOI, ISDELETE, GHICHU FROM NHACUNGCAP WHERE (MANHACUNGCAP = @MANHACUNGCAP)";
	        _SQLSelectByKey = query;

            query = @"UPDATE NHACUNGCAP SET TENNHACUNGCAP = @TENNHACUNGCAP, DIACHI = @DIACHI, DIENTHOAI = @DIENTHOAI, DIDONG = @DIDONG, NGUOIDAIDIEN = @NGUOIDAIDIEN, EMAIL = @EMAIL, THEODOI = @THEODOI, ISDELETE = @ISDELETE, GHICHU = @GHICHU WHERE (MANHACUNGCAP = @MANHACUNGCAP)";
	        _SQLUpdate = query;

            query = @"INSERT INTO NHACUNGCAP VALUES (@TENNHACUNGCAP, @DIACHI, @DIENTHOAI, @DIDONG, @NGUOIDAIDIEN, @EMAIL, @THEODOI, @ISDELETE, @GHICHU)";
	        _SQLInsert = query;

            query = @"DELETE FROM NHACUNGCAP WHERE (MANHACUNGCAP = @MANHACUNGCAP)";
	        _SQLDelete = query;

            query = @"SELECT MANHACUNGCAP, TENNHACUNGCAP, DIACHI, DIENTHOAI, DIDONG, NGUOIDAIDIEN, EMAIL, THEODOI, ISDELETE, GHICHU FROM NHACUNGCAP";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(NHACUNGCAP obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "MANHACUNGCAP", obj.MANHACUNGCAP, DbType.Int32);
                         DalTools.addDbDataParameter(command, "TENNHACUNGCAP", obj.TENNHACUNGCAP, DbType.String);
         DalTools.addDbDataParameter(command, "DIACHI", obj.DIACHI, DbType.String);
         DalTools.addDbDataParameter(command, "DIENTHOAI", obj.DIENTHOAI, DbType.String);
         DalTools.addDbDataParameter(command, "DIDONG", obj.DIDONG, DbType.String);
         DalTools.addDbDataParameter(command, "NGUOIDAIDIEN", obj.NGUOIDAIDIEN, DbType.String);
         DalTools.addDbDataParameter(command, "EMAIL", obj.EMAIL, DbType.String);
         DalTools.addDbDataParameter(command, "THEODOI", obj.THEODOI, DbType.Boolean);
         DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean);
         DalTools.addDbDataParameter(command, "GHICHU", obj.GHICHU, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(NHACUNGCAP obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "MANHACUNGCAP", obj.MANHACUNGCAP, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(NHACUNGCAP obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(NHACUNGCAP obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private NHACUNGCAPList SelectListByCommand(IDbCommand command)
        {
            NHACUNGCAPList list = new NHACUNGCAPList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				NHACUNGCAP obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new NHACUNGCAP();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private NHACUNGCAPList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            NHACUNGCAPList list = new NHACUNGCAPList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				NHACUNGCAP obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new NHACUNGCAP();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(NHACUNGCAP obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["MANHACUNGCAP"] is DBNull))
		{
			obj.MANHACUNGCAP = (Int32)reader["MANHACUNGCAP"];
		}
                if(!(reader["TENNHACUNGCAP"] is DBNull))
		{
			obj.TENNHACUNGCAP = (string)reader["TENNHACUNGCAP"];
		}
                if(!(reader["DIACHI"] is DBNull))
		{
			obj.DIACHI = (string)reader["DIACHI"];
		}
                if(!(reader["DIENTHOAI"] is DBNull))
		{
			obj.DIENTHOAI = (string)reader["DIENTHOAI"];
		}
                if(!(reader["DIDONG"] is DBNull))
		{
			obj.DIDONG = (string)reader["DIDONG"];
		}
                if(!(reader["NGUOIDAIDIEN"] is DBNull))
		{
			obj.NGUOIDAIDIEN = (string)reader["NGUOIDAIDIEN"];
		}
                if(!(reader["EMAIL"] is DBNull))
		{
			obj.EMAIL = (string)reader["EMAIL"];
		}
                if(!(reader["THEODOI"] is DBNull))
		{
			obj.THEODOI = (bool)reader["THEODOI"];
		}
                if(!(reader["ISDELETE"] is DBNull))
		{
			obj.ISDELETE = (bool)reader["ISDELETE"];
		}
                if(!(reader["GHICHU"] is DBNull))
		{
			obj.GHICHU = (string)reader["GHICHU"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public NHACUNGCAP SelectByKey(Int32 MANHACUNGCAP)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MANHACUNGCAP", MANHACUNGCAP, DbType.Int32);
      	    NHACUNGCAPList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public NHACUNGCAP SelectByKey(Int32 MANHACUNGCAP, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MANHACUNGCAP", MANHACUNGCAP, DbType.Int32);
      	    NHACUNGCAPList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public NHACUNGCAPList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    NHACUNGCAPList list =  SelectListByCommand(command);
	        return list;
        }

         public NHACUNGCAPList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    NHACUNGCAPList list =  SelectListByCommand(command);
	        return list;
        }

        public NHACUNGCAPList SelectByCondition(NHACUNGCAPConditionForm condt)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM NHACUNGCAP " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            NHACUNGCAPList list = SelectListByCommand(command);
            return list;
        }

        private void DeleteByCondition(NHACUNGCAPConditionForm condt, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM NHACUNGCAP " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(NHACUNGCAPConditionForm obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetMANHACUNGCAP)
	{
    	    s += " AND MANHACUNGCAP = @MANHACUNGCAP ";
	}
	if (obj.IsSetTENNHACUNGCAP)
	{
    	    s += " AND TENNHACUNGCAP = @TENNHACUNGCAP ";
	}
	if (obj.IsSetDIACHI)
	{
    	    s += " AND DIACHI = @DIACHI ";
	}
	if (obj.IsSetDIENTHOAI)
	{
    	    s += " AND DIENTHOAI = @DIENTHOAI ";
	}
	if (obj.IsSetDIDONG)
	{
    	    s += " AND DIDONG = @DIDONG ";
	}
	if (obj.IsSetNGUOIDAIDIEN)
	{
    	    s += " AND NGUOIDAIDIEN = @NGUOIDAIDIEN ";
	}
	if (obj.IsSetEMAIL)
	{
    	    s += " AND EMAIL = @EMAIL ";
	}
	if (obj.IsSetTHEODOI)
	{
    	    s += " AND THEODOI = @THEODOI ";
	}
	if (obj.IsSetISDELETE)
	{
    	    s += " AND ISDELETE = @ISDELETE ";
	}
	if (obj.IsSetGHICHU)
	{
    	    s += " AND GHICHU = @GHICHU ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, NHACUNGCAPConditionForm obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetMANHACUNGCAP)
            {
            	DalTools.addDbDataParameter(command, "MANHACUNGCAP", obj.MANHACUNGCAP, DbType.Int32); 
            }
            if (obj.IsSetTENNHACUNGCAP)
            {
            	DalTools.addDbDataParameter(command, "TENNHACUNGCAP", obj.TENNHACUNGCAP, DbType.String); 
            }
            if (obj.IsSetDIACHI)
            {
            	DalTools.addDbDataParameter(command, "DIACHI", obj.DIACHI, DbType.String); 
            }
            if (obj.IsSetDIENTHOAI)
            {
            	DalTools.addDbDataParameter(command, "DIENTHOAI", obj.DIENTHOAI, DbType.String); 
            }
            if (obj.IsSetDIDONG)
            {
            	DalTools.addDbDataParameter(command, "DIDONG", obj.DIDONG, DbType.String); 
            }
            if (obj.IsSetNGUOIDAIDIEN)
            {
            	DalTools.addDbDataParameter(command, "NGUOIDAIDIEN", obj.NGUOIDAIDIEN, DbType.String); 
            }
            if (obj.IsSetEMAIL)
            {
            	DalTools.addDbDataParameter(command, "EMAIL", obj.EMAIL, DbType.String); 
            }
            if (obj.IsSetTHEODOI)
            {
            	DalTools.addDbDataParameter(command, "THEODOI", obj.THEODOI, DbType.Boolean); 
            }
            if (obj.IsSetISDELETE)
            {
            	DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean); 
            }
            if (obj.IsSetGHICHU)
            {
            	DalTools.addDbDataParameter(command, "GHICHU", obj.GHICHU, DbType.String); 
            }
        }
        #endregion
    }
}